package com.xiaokui.nursing.service.impl;

import java.util.Arrays;
import java.util.List;
import com.xiaokui.common.utils.DateUtils;
import com.xiaokui.nursing.vo.NursingProjectVo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.xiaokui.nursing.mapper.NursingProjectMapper;
import com.xiaokui.nursing.domain.NursingProject;
import com.xiaokui.nursing.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理项目Service业务层处理
 * 
 * @author alexis
 * @date 2025-06-02
 */
@Service
@AllArgsConstructor
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{

    private NursingProjectMapper nursingProjectMapper;
    private RedisTemplate redisTemplate;
    private static final String CACHE_KEY_PREFIX="nursing:project";

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        int flag=save(nursingProject) ? 1 : 0;;
        deleteCache();
        return flag;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        int flag=updateById(nursingProject) ? 1 : 0;
        deleteCache();
        return flag;
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        int flag=removeByIds(Arrays.asList(ids)) ? 1 : 0;
        deleteCache();
        return flag;
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        int flag=removeById(id) ? 1 : 0;
        deleteCache();
        return flag;
    }

    /**
     * 查询所有护理项目
     *
     * @return 护理项目列表
     */
    @Override
    public List<NursingProjectVo> getAll() {
        List<NursingProjectVo> list=(List<NursingProjectVo>)redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if(list!=null&&list.size()>0){
            return list;
        }
        list=nursingProjectMapper.getAll();
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX,list);
        return list;

    }

    /**
     * 删除缓存
     */
    private void deleteCache(){
        redisTemplate.delete(CACHE_KEY_PREFIX);
    }
}
